home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_10_09
/
1009099a
< prev
next >
Wrap
Text File
|
1992-07-06
|
706b
|
42 lines
Listing 1
#include <iostream.h>
class X
{
friend ostream &operator<<(ostream &os, const X &x);
public:
X(int i) : a(i), b(0) { }
X(X &x) : a(x.a), b(x.b) { ++x.b; }
X(const X &x) : a(x.a), b(x.b+1) { }
private:
int a, b;
};
ostream &operator<<(ostream &os, const X &x)
{
return os << '(' << x.a << ", " << x.b << ')';
}
int main()
{
X x1(3); // uses X(int)
cout << "x1 = " << x1 << '\n';
const X x2(x1); // uses X(X &)
cout << "x1 = " << x1 << '\n';
cout << "x2 = " << x2 << '\n';
X x3(x2); // uses X(const X &)
cout << "x2 = " << x2 << '\n';
cout << "x3 = " << x3 << '\n';
return 0;
}
----------